import os

from dateutil.utils import today
from django.http import JsonResponse
from django.shortcuts import render
from django.views import View
from haystack.views import SearchView

from meiduo_mall import settings
from utils.goods import *
from apps.contents.models import *
from .models import *
from django.core.paginator import Paginator
class IndexView(View):

    def get(self, request):
        categories = get_categories()
        contents = {}
        content_categories = ContentCategory.objects.all()
        for cat in content_categories:
            contents[cat.key]= cat.content_set.filter(status=True).order_by('sequence')

        context = {
            'categories': categories,
            'contents': contents,
        }

        return render(request, 'index.html', context)

class ListView(View):

    def get(self,request,category_id):
        ordering=request.GET.get('ordering')
        page_size=request.GET.get('page_size')
        page=request.GET.get('page')

        try:
            category=GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return JsonResponse({'code':400,'errmsg':'参数缺失'})

        breadcrumb=get_breadcrumb(category)
        skus=SKU.objects.filter(category=category,is_launched=True).order_by(ordering)

        from django.core.paginator import Paginator
        paginator=Paginator(skus,per_page=page_size)
        page_skus=paginator.page(page)
        sku_list=[]

        for sku in page_skus.object_list:
            sku_list.append({
                'id':sku.id,
                'name':sku.name,
                'price':sku.price,
                'default_image_url':sku.default_image.url
            })
        total_num = paginator.num_pages
        return JsonResponse({'code':0,'errmsg':'ok','list':sku_list,'count':total_num,'breadcrumb':breadcrumb})

class HotGoodsView(View):

    def get(self,request,category_id):
        skus=SKU.objects.filter(category_id=category_id,is_launched=True).order_by('-sales')[:2]

        hot_skus=[]
        for sku in skus:
            hot_skus.append({
                'id':sku.id,
                'name':sku.name,
                'price':sku.price,
                'default_image_url':sku.default_image.url
            })

        return JsonResponse({'code':0,'errmsg':'ok','hot_skus':hot_skus})


class SKUSearchView(SearchView):

    def create_response(self):
        context = self.get_context()
        sku_list=[]
        for sku in context['page'].object_list:
            sku_list.append({
                'id':sku.object.id,
                'name':sku.object.name,
                'price':sku.object.price,
                'default_image_url':sku.object.default_image.url,
                'searchkey':context.get('query'),
                'page_size':context['page'].paginator.num_pages,
                'count':context['page'].paginator.count
            })

        return JsonResponse(sku_list,safe=False)

class DetailView(View):
    def get(self,request,sku_id):

        try:
            sku=SKU.objects.get(id=sku_id)
        except SKU.DoesNotExist:
            return JsonResponse({'code':400,'errmsg':'参数缺失'})

        categories = get_categories()
        breadcrumb=get_breadcrumb(sku.category)
        goods_specs=get_goods_specs(sku)

        context = {
            'categories': categories,
            'breadcrumb': breadcrumb,
            'sku': sku,
            'specs': goods_specs,
        }

        return render(request,'detail.html',context)

import datetime
class CategoryVisitCountView(View):

    def post(self,request,category_id):

        try:
            category=GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return JsonResponse({'code':400,'errmsg':'参数缺失'})

        today=datetime.date.today()
        try:
            gvc=GoodsVisitCount.objects.get(category=category,date=today)
        except GoodsVisitCount.DoesNotExist:
            gvc=GoodsVisitCount.objects.create(category=category,date=today,count=1)
        else:
            gvc.count+=1
            gvc.save()

        return JsonResponse({'code':0,'errmsg':'ok'})
